package com.xjm.ips.controller.employee;

import com.xjm.ips.entity.*;
import com.xjm.ips.service.*;
import com.xjm.ips.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/employee/basic")
public class EmployeeBasicController {
    @Autowired
    EmployeeBasiService employeeBasiService;
    @Autowired
    NationService nationService;
    @Autowired
    PoliticsstatusService politicsstatusService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    JobLevelService jobLevelService;
    @Autowired
    PositionService positionService;


    /**
     * 分页查询员工资料，传页数和条数，不传则默认第一页10条
     */
    @GetMapping("/")
    public MyPage getEmployee(@RequestParam(defaultValue = "1") Integer pageNo, @RequestParam(defaultValue = "10") Integer pageSize, Employee employee){
            return employeeBasiService.getEmployee(pageNo,pageSize,employee);
    }

    @PostMapping("/")
    public AjaxJson addEmployee(@RequestBody Employee employee){
        if(employeeBasiService.addEmployee(employee)==1){
            return AjaxJson.ok("添加员工信息成功!");
        }
        return AjaxJson.error("添加员工信息失败!");
    }

    @GetMapping("/getallnations")
    public List<Nation> getAllNations(){
        return nationService.getAllNations();
    }

    @GetMapping("/getallpolstatus")
    public List<Politicsstatus> getAllPolStatus(){
        return politicsstatusService.getAllPolStatus();
    }

    @GetMapping("/getalljoblevels")
    public List<JobLevel> getAllJobLevels(){
        return jobLevelService.getAllJobLevel();
    }

    @GetMapping("/getallpositions")
    public List<Position> getAllPositions(){
        return positionService.getAllPositions();
    }

    @GetMapping("/getjobno")
    public AjaxJson getJobNo(){
        /* 查询数据库最大的工号，+1返回给前端，格式化为8位数 */
        AjaxJson ajaxJson = AjaxJson.build().setStatus(200).
                setObj(String.format("%08d", employeeBasiService.getjobNo() + 1));
        return ajaxJson;
    }

    @GetMapping("/getalldepartments")
    public List<Department> getAllDepartments(){
        return departmentService.getAllDepartments();
    }

    @DeleteMapping("/{id}")
    public AjaxJson deleteEmployeeById(@PathVariable Integer id) {
        if (employeeBasiService.deleteEmployeeById(id) == 1) {
            return AjaxJson.ok("删除员工信息成功!");
        }
        return AjaxJson.error("删除员工信息失败!");
    }

    @DeleteMapping("/batchDeleteEmployeeByIds")
    public AjaxJson batchDeleteEmployeeByIds(Integer[] ids) {
        if(employeeBasiService.batchDeleteEmployeeByIds(ids) == ids.length){
            return AjaxJson.ok("批量删除成功!");
        }
        return AjaxJson.error("批量删除失败!");
    }

    @PutMapping("/")
    public AjaxJson updateEmployee(@RequestBody Employee employee){
        if(employeeBasiService.updateEmployee(employee)==1){
            return AjaxJson.ok("修改员工信息成功!");
        }
        return AjaxJson.error("修改员工信息失败!");
    }

    @RequestMapping("/export")
    public ResponseEntity<byte[]> exportEmployeeData(@RequestParam(defaultValue = "1") Integer pageNo, @RequestParam(defaultValue = "10")
            Integer pageSize){
        List<Employee> employees = (List<Employee>) employeeBasiService.getEmployee(pageNo, pageSize, null)
                .getData();
        return ExcelUtils.handleEmployeeToExcel(employees);
    }

    @PostMapping("/import")
    public AjaxJson importEmployeeData(MultipartFile file) throws IOException {
//        file.transferTo(new File("D:\\vhr.xls"));
        List<Employee> list = ExcelUtils.importEmployeeData(file,nationService.getAllNations(),
                positionService.getAllPositions(),politicsstatusService.getAllPolStatus(),departmentService.getAllDepartNoChildren(),
                jobLevelService.getAllJobLevel());
        if(employeeBasiService.addEmployees(list)==list.size()){
            return AjaxJson.ok("文件导入成功！");
        }
        return AjaxJson.error("文件导入失败!");
    }
}
